﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GrimorioApplication.Localization;
using GrimorioApplication.Model;

namespace GrimorioApplication.Views
{
    public partial class SearchRecipe : PhoneApplicationPage
    {
        public SearchRecipe()
        {
            InitializeComponent();

            listPickerDifficulty.ItemsSource = new List<string> {
                GrimorioLang.listpiker_element_ALL,
                GrimorioLang.listpicker_difficulty_easy,
                GrimorioLang.listpicker_difficulty_medium,
                GrimorioLang.listpicker_difficulty_hard
            };

            listPickerDish.ItemsSource = new List<string> { 
                GrimorioLang.listpiker_element_ALL,
                GrimorioLang.listpicker_dish_antipasto, 
                GrimorioLang.listpicker_dish_primo,
                GrimorioLang.listpicker_dish_secondo,
                GrimorioLang.listpicker_dish_contorno,
                GrimorioLang.listpicker_dish_dolce 
            };
        }

        private void boxOfSearchRecipe_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateList();
        }

        private void listPickerDish_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateList();
        }

        private void listPickerDifficulty_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateList();
        }
        private void UpdateList()
        {
            string difficulty = (string)listPickerDifficulty.SelectedItem;
            string dish = (string)listPickerDish.SelectedItem;
            string starts = boxOfSearchRecipe.Text;
            if (difficulty == GrimorioLang.listpiker_element_ALL)
            {
                if (dish == GrimorioLang.listpiker_element_ALL)
                    searchList.ItemsSource = App.ViewModel.getRecipeListBeginWith(starts);
                else
                    searchList.ItemsSource = App.ViewModel.getRecipeListBeginWith(starts, dish, 0);
            }
            else
            {
                if (dish == GrimorioLang.listpiker_element_ALL)
                    searchList.ItemsSource = App.ViewModel.getRecipeListBeginWith(starts, difficulty, 1);
                else
                    searchList.ItemsSource = App.ViewModel.getRecipeListBeginWith(starts, difficulty, dish);
            }
            

        }

        private void searchList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRecipe loadThis = (DataRecipe)searchList.SelectedItem;
            if(loadThis!=null)
                NavigationService.Navigate(new Uri("/Views/ViewRecipeDetails.xaml?" + "ID=" + loadThis.RecipeId.ToString(), UriKind.Relative));
        }
    }
}